package com.hckjsec.system.controller;

import java.util.List;

import com.hckjsec.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hckjsec.common.annotation.Log;
import com.hckjsec.common.core.controller.BaseController;
import com.hckjsec.common.core.domain.AjaxResult;
import com.hckjsec.common.enums.BusinessType;
import com.hckjsec.system.domain.SysDispatch;
import com.hckjsec.system.service.ISysDispatchService;
import com.hckjsec.common.utils.poi.ExcelUtil;
import com.hckjsec.common.core.page.TableDataInfo;

/**
 * 发文Controller
 * 
 * @author cyt
 * @date 2021-10-11
 */
@RestController
@RequestMapping("/system/dispatch")
public class SysDispatchController extends BaseController
{
    @Autowired
    private ISysDispatchService sysDispatchService;

    /**
     * 查询发文列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysDispatch sysDispatch)
    {
        startPage();
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            sysDispatch.setDeptId(SecurityUtils.getDeptId());
        }
        List<SysDispatch> list = sysDispatchService.selectSysDispatchList(sysDispatch);
        return getDataTable(list);
    }

    /**
     * 导出发文列表
     */
    @Log(title = "发文", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysDispatch sysDispatch)
    {
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            sysDispatch.setDeptId(SecurityUtils.getDeptId());
        }
        List<SysDispatch> list = sysDispatchService.selectSysDispatchList(sysDispatch);
        ExcelUtil<SysDispatch> util = new ExcelUtil<SysDispatch>(SysDispatch.class);
        return util.exportExcel(list, "dispatch");
    }

    /**
     * 获取发文详细信息
     */
    @GetMapping(value = "/{dispatchId}")
    public AjaxResult getInfo(@PathVariable("dispatchId") Long dispatchId)
    {
        return AjaxResult.success(sysDispatchService.selectSysDispatchById(dispatchId));
    }

    /**
     * 新增发文
     */
    @Log(title = "发文", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysDispatch sysDispatch)
    {
        sysDispatch.setDeptId(SecurityUtils.getDeptId());
        sysDispatch.setOperator(SecurityUtils.getUserId());
        return toAjax(sysDispatchService.insertSysDispatch(sysDispatch));
    }

    /**
     * 修改发文
     */
    @Log(title = "发文", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysDispatch sysDispatch)
    {
        return toAjax(sysDispatchService.updateSysDispatch(sysDispatch));
    }

    /**
     * 删除发文
     */
    @Log(title = "发文", businessType = BusinessType.DELETE)
	@DeleteMapping("/{dispatchIds}")
    public AjaxResult remove(@PathVariable Long[] dispatchIds)
    {
        return toAjax(sysDispatchService.deleteSysDispatchByIds(dispatchIds));
    }
}
